#include "vtk_crossline.h".h"

vtk_CrossLine::vtk_CrossLine(unsigned short _type)
    : type(_type)
    , vline(vtkSmartPointer<vtkLineSource>::New())
    , hline(vtkSmartPointer<vtkLineSource>::New())
    , cross(vtkSmartPointer<vtkAppendPolyData>::New())
    , mapper(vtkSmartPointer<vtkDataSetMapper>::New())
    , actor(vtkSmartPointer<vtkActor>::New())
{
    cross->AddInputConnection(vline->GetOutputPort());
    cross->AddInputConnection(hline->GetOutputPort());
    mapper->AddInputConnection(cross->GetOutputPort());
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(color);
    actor->GetProperty()->SetLineWidth(lineWidth);
}

void vtk_CrossLine::setCenter(double _center[])
{
    if(0 == type) {
        setCenter(_center[1], _center[2]);
    } else if(1 == type) {
        setCenter(_center[0], _center[2]);
    } else if(2 == type) {
        setCenter(_center[0], _center[1]);
    }
}

void vtk_CrossLine::setCenter(double _wx, double _wy)
{
    _wx = _wx < 0 ? 0 :
          _wx > width ? width :
          _wx;
    _wy = _wy < 0 ? 0 :
          _wy > height ? height :
          _wy;
    if(0 == type) {
        center[1] = _wx;
        center[2] = _wy;
        vline->SetPoint1(center[0], 0, center[2]);
        vline->SetPoint2(center[0], width, center[2]);
        hline->SetPoint1(center[0], center[1], 0);
        hline->SetPoint2(center[0], center[1], height);
    } else if(1 == type) {
        center[0] = _wx;
        center[2] = _wy;
        vline->SetPoint1(0, center[1], center[2]);
        vline->SetPoint2(width, center[1], center[2]);
        hline->SetPoint1(center[0], center[1], 0);
        hline->SetPoint2(center[0], center[1], height);
    } else if(2 == type) {
        center[0] = _wx;
        center[1] = _wy;
        vline->SetPoint1(0, center[1], center[2]);
        vline->SetPoint2(width, center[1], center[2]);
        hline->SetPoint1(center[0], 0, center[2]);
        hline->SetPoint2(center[0], height, center[2]);
    }
    cross->Update();
}

void vtk_CrossLine::setSize(const img_Info *info)
{
    if(0 == type) {
        setSize(
            info->dim[3]*info->pixdim[3],
            info->dim[2]*info->pixdim[2]
        );
    } else if(1 == type) {
        setSize(
            info->dim[3]*info->pixdim[3],
            info->dim[1]*info->pixdim[1]
        );
    } else if(2 == type) {
        setSize(
            info->dim[1]*info->pixdim[1],
            info->dim[2]*info->pixdim[2]
        );
    }
}

void vtk_CrossLine::setSize(double _height, double _width)
{
    height = _height;
    width = _width;
    setCenter(width / 2.0, height / 2.0);
}

void vtk_CrossLine::setColor(double _r, double _g, double _b)
{
    color[0] = _r;
    color[1] = _g;
    color[2] = _b;
    actor->GetProperty()->SetColor(color);
}

void vtk_CrossLine::setLineWidth(float _lw)
{
    lineWidth = _lw;
    actor->GetProperty()->SetLineWidth(_lw);
}

void vtk_CrossLine::getCenter(double _center[3])
{
    _center[0] = center[0];
    _center[1] = center[1];
    _center[2] = center[2];
}

vtkActor *vtk_CrossLine::getActor()
{
    return actor;
}
